import settings
from httpx import post, get

class BaseApi:
    # 从 django settings 获取蓝鲸应用信息（app_code=settings.APP_CODE，app_secret=settings.SECRET_KEY）
    # 参数 request 为 django Request 对象，其中包含用户信息
    def requests(self, method: str, url: str, data: dict = {}) -> dict:
        try:
            req_method = method.lower()
            if req_method == 'get':
                req = get(url=url, verify=False)
            elif req_method == 'post':
                data["bk_username"] = settings.BK_USERNAME  # 可以不需要
                data["bk_token"] = settings.BK_TOKEN
                data["bk_app_code"] = settings.BK_APP_CODE
                data["bk_app_secret"] = settings.BK_APP_SECRET
                req = post(url="{0}{1}/".format(settings.MONITOR_API_URL, url), json=data, verify=False)
                return req.json()
        except Exception as e:
            return {"result": False, "code": 0, "data": [], "message": e, "permission": e}

